/**
******************************************************************************
* @file           : task_alog_c.cpp
* @author         : wyj
* @brief          : C语言语法测试
* @attention      : None
* @date           : 2025/5/9
******************************************************************************
*/

#ifdef __cplusplus
extern "C"
{
#endif

#include "control/pid/studio_pid_c.h"
#include "geography/studio_proj_c.h"
#include "geometry/studio_geo_algo_c.h"

#ifdef __cplusplus
}
#endif

#include <stdio.h>
#include <stdio.h>
#include "geometry/studio_geo_utils.h"
#include "file/studio_dir.h"

void studio_line_to_c_line(const studio_line& line, studio_line_c* c_line)
{
    for (size_t i = 0; i < line.size(); ++i)
    {
        studio_point_c p = studio_point_init(line[i].x, line[i].y);
        studio_line_c_add_point(c_line, p);
    }
}

void c_line_to_studio_line(const studio_line_c* c_line, studio_line& line)
{
    for (size_t i = 0; i < c_line->size; ++i)
    {
        const studio_point_c* p = studio_line_c_get_point(c_line, i);
        line.push_back(studio_point(p->x, p->y));
    }
}

///////////////////////////////// 以上是工具转化函数 //////////////////////////////////



////////////////// 路径压缩算法测试 /////////////////////
int main()
{
    printf("\n\n===================== %s =====================\n\n", __FILE__);
    studio_geo_utils::init_gdal_env();

    std::string src_root;
    std::string dst_root;
#ifdef IS_WINDOWS
    src_root = "D:/9_data/2_readfile/geojson/path_compress/";
    dst_root = "D:/9_data/2_readfile/geojson/path_compress/dst/";
#else
    src_root = "/home/wyj/myself/2_data/2_geojson/path_compress/";
    dst_root = "/home/wyj/myself/2_data/2_geojson/path_compress/";
#endif

    std::vector<std::string> all_paths = studio_dir::getFilesOrDirs(src_root);

    for (const auto& path : all_paths)
    {
        std::cout << "src path: " << path << std::endl;
        std::vector<studio_geo_coll> res_collections;
        std::vector<studio_geo_coll> collections;
        studio_geo_utils::read_geo_coll(path, collections);
        for (auto& coll : collections)
        {
            studio_line_c line_c = studio_line_c_init();
            studio_line_c vac_line_c = studio_line_c_init();
            studio_line_to_c_line(coll.m_line, &line_c);
            std::cout << "原始数据量：" << line_c.size << std::endl;
            // 简化线段，目标点数为30个
            int max_points = 30;
            double epsilon = 1.0;
            bool res = line_vacuate_gcs_c(&line_c, max_points, epsilon, &vac_line_c);
            if (!res)
            {
                std::cout << "Failed to simplify line." << std::endl;
                return 1;
            }
            studio_line simplified_line;
            c_line_to_studio_line(&vac_line_c, simplified_line);
            std::cout << "简化后数据量：" << vac_line_c.size << std::endl;

            studio_geo_coll temp;
            if (simplified_line.size() > 0)
            {
                temp.m_type = enum_geometry_type::egtLineString;
                temp.m_line = simplified_line;
                res_collections.push_back(temp);
            }

            studio_line_c_destroy(&line_c);
            studio_line_c_destroy(&vac_line_c);

            break;
        }

        std::string name = std::filesystem::path(path).filename().stem().string();
        std::string output_path = std::filesystem::path(dst_root).append(name + "_dst.geojson").string();
        studio_geo_utils::write_geo_coll(output_path, res_collections);
    }

    studio_geo_utils::destroy_gdal_env();
    return 0;
}






//// ///////////////////// 路径压缩错误测试 /////////////////////
//
// int main()
//{
//    printf("\n\n===================== %s =====================\n\n", __FILE__);
//    studio_geo_utils::init_gdal_env();
//    std::string path;
// #ifdef IS_WINDOWS
//    path = "D:/5_file/2_readfile/geojson/multi_point/fitting_examples.geojson";
// #else
//    path = "/home/wyj/myself/2_data/2_geojson/multi_point/fitting_examples.geojson";
// #endif
//
//    std::cout << "path: " << path << std::endl;
//    std::vector<studio_geo_coll> res_collections;
//    std::vector<studio_geo_coll> collections;
//
//    // ------------- 转换为高斯投影 -------------
//    studio_line gauss_line;
//    double central = static_cast<int>(120.039001 / 3) * 3;
//
//    studio_line_c gcs_line_c = studio_line_c_init();
//    studio_line_c vac_gcs_line_c = studio_line_c_init();
//
//    studio_line_c_add_point(&gcs_line_c, studio_point_init(120.039001, 30.982000));
//    studio_line_c_add_point(&gcs_line_c, studio_point_init(120.380996, 30.843999));
//    studio_line_c_add_point(&gcs_line_c, studio_point_init(120.469001, 30.371999));
//    studio_line_c_add_point(&gcs_line_c, studio_point_init(120.453002, 30.420000));
//    studio_line_c_add_point(&gcs_line_c, studio_point_init(120.277000, 30.131999));
//    studio_line_c_add_point(&gcs_line_c, studio_point_init(120.691001, 30.601999));
//    studio_line_c_add_point(&gcs_line_c, studio_point_init(120.210998, 30.250000));
//    studio_line_c_add_point(&gcs_line_c, studio_point_init(120.544998, 30.871999));
//    studio_line_c_add_point(&gcs_line_c, studio_point_init(120.752998, 30.440000));
//    studio_line_c_add_point(&gcs_line_c, studio_point_init(120.864997, 30.0000000));
//    studio_line_c_add_point(&gcs_line_c, studio_point_init(120.102996, 30.125999));
//    studio_line_c_add_point(&gcs_line_c, studio_point_init(120.766998, 30.365999));
//    studio_line_c_add_point(&gcs_line_c, studio_point_init(120.583000, 30.158000));
//    studio_line_c_add_point(&gcs_line_c, studio_point_init(120.663002, 30.326000));
//    studio_line_c_add_point(&gcs_line_c, studio_point_init(120.406997, 30.445999));
//    studio_line_c_add_point(&gcs_line_c, studio_point_init(120.516998, 30.131999));
//    studio_line_c_add_point(&gcs_line_c, studio_point_init(120.972999, 30.555999));
//    studio_line_c_add_point(&gcs_line_c, studio_point_init(120.654998, 30.253999));
//    studio_line_c_add_point(&gcs_line_c, studio_point_init(120.838996, 30.270000));
//    studio_line_c_add_point(&gcs_line_c, studio_point_init(120.855003, 30.110000));
//    studio_line_c_add_point(&gcs_line_c, studio_point_init(120.060997, 30.131999));
//    studio_line_c_add_point(&gcs_line_c, studio_point_init(120.925003, 30.139999));
//    studio_line_c_add_point(&gcs_line_c, studio_point_init(120.845001, 30.299999));
//    studio_line_c_add_point(&gcs_line_c, studio_point_init(120.189002, 30.299999));
//    studio_line_c_add_point(&gcs_line_c, studio_point_init(120.861000, 30.211999));
//    studio_line_c_add_point(&gcs_line_c, studio_point_init(120.154998, 30.722000));
//    studio_line_c_add_point(&gcs_line_c, studio_point_init(120.306999, 30.610000));
//    studio_line_c_add_point(&gcs_line_c, studio_point_init(120.302001, 30.745000));
//    studio_line_c_add_point(&gcs_line_c, studio_point_init(120.152999, 30.079999));
//    studio_line_c_add_point(&gcs_line_c, studio_point_init(120.425003, 30.656000));
//    studio_line_c_add_point(&gcs_line_c, studio_point_init(120.782997, 30.054000));
//    studio_line_c_add_point(&gcs_line_c, studio_point_init(120.151000, 30.517999));
//    studio_line_c_add_point(&gcs_line_c, studio_point_init(120.303001, 30.558000));
//    studio_line_c_add_point(&gcs_line_c, studio_point_init(120.238998, 30.733999));
//  //     studio_line_c_add_point(&gcs_line_c, studio_point_init(120.07207105000001,36.15950345000000));
//  // studio_line_c_add_point(&gcs_line_c, studio_point_init(120.07206542000000,36.15950579000000));
//  // studio_line_c_add_point(&gcs_line_c, studio_point_init(120.07205912000001,36.15950948000000));
//  // studio_line_c_add_point(&gcs_line_c, studio_point_init(120.07205174000001,36.15951394000000));
//  // studio_line_c_add_point(&gcs_line_c, studio_point_init(120.07204096000000,36.15952022000000));
//  // studio_line_c_add_point(&gcs_line_c, studio_point_init(120.07203156000000,36.15952566000000));
//  // studio_line_c_add_point(&gcs_line_c, studio_point_init(120.07202411999999,36.15952983000000));
//  // studio_line_c_add_point(&gcs_line_c, studio_point_init(120.07200829000000,36.15953788000000));
//  // studio_line_c_add_point(&gcs_line_c, studio_point_init(120.07199709000000,36.15954257000000));
//  // studio_line_c_add_point(&gcs_line_c, studio_point_init(120.07198529000000,36.15954627000000));
//  // studio_line_c_add_point(&gcs_line_c, studio_point_init(120.07172613000000,36.15949536000000));
//  // studio_line_c_add_point(&gcs_line_c, studio_point_init(120.07171595000000,36.15949109000000));
//  // studio_line_c_add_point(&gcs_line_c, studio_point_init(120.07170811000000,36.15948855000000));
//  // studio_line_c_add_point(&gcs_line_c, studio_point_init(120.07169195000000,36.15948536000000));
//  // studio_line_c_add_point(&gcs_line_c, studio_point_init(120.07168092000001,36.15948474000000));
//  // studio_line_c_add_point(&gcs_line_c, studio_point_init(120.07166998000000,36.15948535000000));
//  // studio_line_c_add_point(&gcs_line_c, studio_point_init(120.07166058000000,36.15948630000000));
//  // studio_line_c_add_point(&gcs_line_c, studio_point_init(120.07165083000000,36.15948751000000));
//  // studio_line_c_add_point(&gcs_line_c, studio_point_init(120.07164358999999,36.15948821000000));
//  // studio_line_c_add_point(&gcs_line_c, studio_point_init(120.07163668000000,36.15948832000000));
//  // studio_line_c_add_point(&gcs_line_c, studio_point_init(120.07155865000000,36.15941322000000));
//  // studio_line_c_add_point(&gcs_line_c, studio_point_init(120.07155723000000,36.15940347000000));
//  // studio_line_c_add_point(&gcs_line_c, studio_point_init(120.07155523000000,36.15939333000000));
//  // studio_line_c_add_point(&gcs_line_c, studio_point_init(120.07155331000000,36.15938564000000));
//  // studio_line_c_add_point(&gcs_line_c, studio_point_init(120.07154843000001,36.15937024000000));
//  // studio_line_c_add_point(&gcs_line_c, studio_point_init(120.07154410000000,36.15936034000000));
//  // studio_line_c_add_point(&gcs_line_c, studio_point_init(120.07153860000000,36.15935092000000));
//  // studio_line_c_add_point(&gcs_line_c, studio_point_init(120.07153018000000,36.15934010000000));
//  // studio_line_c_add_point(&gcs_line_c, studio_point_init(120.07152281000000,36.15933237000000));
//  // studio_line_c_add_point(&gcs_line_c, studio_point_init(120.07151780000000,36.15932673000000));
//  // studio_line_c_add_point(&gcs_line_c, studio_point_init(120.07151684000000,36.15932353000000));
//  // studio_line_c_add_point(&gcs_line_c, studio_point_init(120.07152006000000,36.15932156000000));
//  // studio_line_c_add_point(&gcs_line_c, studio_point_init(120.07152524000000,36.15932060000000));
//  // studio_line_c_add_point(&gcs_line_c, studio_point_init(120.07153203999999,36.15931997000000));
//
//    std::cout << "befor line size:" << gcs_line_c.size << std::endl;
//    {
//        studio_line stu_line;
//        c_line_to_studio_line(&gcs_line_c, stu_line);
//        studio_geo_coll temp;
//        temp.m_type = enum_geometry_type::egtLineString;
//        temp.m_line = stu_line;
//        collections.push_back(temp);
//        std::string temp_out = "/home/wyj/myself/2_data/2_geojson/multi_point/20250603_aaa.geojson";
//        studio_geo_utils::write_geo_coll(temp_out, collections);
//    }
//
//
//    // 简化线段，目标点数为28个
//    int max_points = 30;
//    double epsilon = 1.0;
//
//    bool res = line_vacuate_gcs_c(&gcs_line_c, max_points, epsilon, &vac_gcs_line_c);
//    if (!res)
//    {
//        std::cout << "Failed to simplify line." << std::endl;
//        return 1;
//    }
//    std::cout << "Simplified line size:" << vac_gcs_line_c.size << std::endl;
//    studio_line simplified_line;
//    c_line_to_studio_line(&vac_gcs_line_c, simplified_line);
//
//    studio_geo_coll temp;
//    temp.m_type = enum_geometry_type::egtLineString;
//    temp.m_line = simplified_line;
//    res_collections.push_back(temp);
//
//    studio_line_c_destroy(&gcs_line_c);
//    studio_line_c_destroy(&vac_gcs_line_c);
//
//    std::string output_path;
// #ifdef IS_WINDOWS
//    output_path = "D:/5_file/2_readfile/geojson/multi_point/fitting_examples_res_1.geojson";
// #else
//    output_path = "/home/wyj/myself/2_data/2_geojson/multi_point/20250603_res.geojson";
// #endif
//    studio_geo_utils::write_geo_coll(output_path, res_collections);
//
//    studio_geo_utils::destroy_gdal_env();
//
//    return 0;
//}


//int main()
//{
//    printf("===================== %s =====================\n", __FILE__);
//
//    // 初始化线段
//    studio_line_c line = studio_line_c_init();
//
//    // 添加点
//    for (int i = 0; i < 10; ++i)
//    {
//        studio_point_c p = studio_point_init(i * 1.0, i * 2.0);
//        studio_line_c_add_point(&line, p);
//    }
//
//    // 遍历点
//    for (size_t i = 0; i < line.size; ++i)
//    {
//        studio_point_c p = studio_line_c_get_point(&line, i);
//        printf("Point %zu: (%.1f, %.1f)\n", i, p.x, p.y);
//    }
//    for (size_t i = 0; i < line.size; ++i)
//    {
//        studio_point_c  p = studio_point_init(111, 111);
//        studio_line_c_set_point(&line, i, p);
//    }
//    for (size_t i = 0; i < line.size; ++i)
//    {
//        studio_point_c p = studio_line_c_get_point(&line, i);
//        printf("Point %zu: (%.1f, %.1f)\n", i, p.x, p.y);
//    }
//
//    // 释放内存
//    studio_line_c_destroy(&line);
//    return 0;
//
//
//
//    return 0;
//}

// ///////////////////// PID算法 /////////////////////
// int main()
// {
//     double ini = 2, goal = 50, p = 0.5, i = 0.001, d = 0.6;
//     studio_pid pid;
//
//     // 初始化PID
//     init_pid(&pid, ini, goal, p, i, d, 100, 200);
//
//     // 第一阶段控制到 50
//     double current_value = ini;
//     for (int i = 0; i < 20; ++i)
//     {
//         double output = compute_pid(&pid, current_value);
//         current_value += output; // 积分项和微分项会影响下一次计算
//         printf("Step %d: 目标=%.2f, 当前值=%.2f, 输出=%.2f\n", i + 1, get_goal(&pid), current_value, output);
//     }
//
//     printf("到达目标值，开始第二阶段控制\n");
//     // 改变目标值并重置PID状态
//     set_goal(&pid, 100);
//
//     // 第二阶段控制到 100
//     for (int i = 0; i < 20; ++i)
//     {
//         double output = compute_pid(&pid, current_value);
//         current_value += output; // 积分项和微分项会影响下一次计算
//         printf("Step %d: 目标=%.2f, 当前值=%.2f, 输出=%.2f\n", i + 1, get_goal(&pid), current_value, output);
//     }
//
//     return 0;
// }

// ///////////////////// 移除离群点 /////////////////////
// int main()
// {
//     printf("\n\n===================== %s =====================\n\n", __FILE__);
//     studio_geo_utils::init_gdal_env();
//     std::string path;
// #ifdef IS_WINDOWS
//     path = "D:/5_file/2_readfile/geojson/multi_point/fitting_examples.geojson";
// #else
//     path = "/home/wyj/myself/2_data/2_geojson/multi_point/fitting_examples.geojson";
// #endif
//
//     std::cout << "path: " << path << std::endl;
//     std::vector<studio_geo_coll> res_collections;
//     std::vector<studio_geo_coll> collections;
//     studio_geo_utils::read_geo_coll(path, collections);
//
//     for (auto &coll: collections)
//     {
//         studio_line_c line_c = studio_line_c_init();
//         studio_line_to_c_line(coll.m_line, &line_c);
//         std::cout << "原始数据量：" << coll.m_line.size() << std::endl;
//
//         studio_line_c outliers_line = studio_line_c_init();
//         remove_outliers_c(&line_c, 1, &outliers_line);
//
//         studio_line simplified_line;
//         c_line_to_studio_line(&outliers_line, simplified_line);
//         std::cout << "简化后数据量：" << simplified_line.size() << std::endl;
//
//         studio_geo_coll temp;
//         temp.m_type = enum_geometry_type::egtLineString;
//         temp.m_line = simplified_line;
//         res_collections.push_back(temp);
//
//         studio_line_c_destroy(&line_c);
//         studio_line_c_destroy(&outliers_line);
//
//         break;
//     }
//
//
//     std::string output_path;
// #ifdef IS_WINDOWS
//     output_path = "D:/5_file/2_readfile/geojson/multi_point/remove_outliers.geojson";
// #else
//     output_path = "/home/wyj/myself/2_data/2_geojson/multi_point/remove_outliers.geojson";
// #endif
//     studio_geo_utils::write_geo_coll(output_path, res_collections);
//      studio_geo_utils::destroy_gdal_env();
//     return 0;
// }


// int main()
//{
//     printf("\n\n===================== %s =====================\n\n", __FILE__);
//     studio_geo_utils::init_gdal_env();
//     std::string path;
// #ifdef IS_WINDOWS
//     path = "D:/5_file/2_readfile/geojson/multi_point/fitting_examples.geojson";
// #else
//     path = "/home/wyj/myself/2_data/2_geojson/multi_point/fitting_examples.geojson";
// #endif

//    std::cout << "path: " << path << std::endl;
//    std::vector<studio_geo_coll> res_collections;
//    std::vector<studio_geo_coll> collections;
//    studio_geo_utils::read_geo_coll(path, collections);
//    for (auto &coll : collections)
//    {
//        // ------------- 转换为高斯投影 -------------
//        studio_line gauss_line;
//        double central = static_cast<int>(coll.m_line[0].x / 3) * 3;
//        for (auto &point : coll.m_line)
//        {
//            double gx = 0.0;
//            double gy = 0.0;
//            lonlat_to_gauss(central, point.x, point.y, &gx, &gy);
//            gauss_line.push_back(studio_point(gx, gy));
//        }
//        studio_line_c gauss_line_c = studio_line_c_init();
//        studio_line_c vac_gauss_line_c = studio_line_c_init();
//        studio_line_to_c_line(gauss_line, &gauss_line_c);

//        // 简化线段，目标点数为28个
//        int max_points = 28;
//        double epsilon = 1.0;

//        bool res = line_vacuate_c(&gauss_line_c, max_points, epsilon, &vac_gauss_line_c);
//        if (!res)
//        {
//            std::cout << "Failed to simplify line." << std::endl;
//            return 1;
//        }

//        // 高斯投影在转回经纬度
//        studio_line simplified_line;
//        for (int i = 0; i < vac_gauss_line_c.size; i++)
//        {
//            double lon = 0.0;
//            double lat = 0.0;
//            const studio_point_c *p = studio_line_c_get_point(&vac_gauss_line_c, i);
//            gauss_to_lonlat(central, p->x, p->y, &lon, &lat);
//            simplified_line.push_back(studio_point(lon, lat));
//        }

//        studio_geo_coll temp;
//        temp.m_type = enum_geometry_type::egtLineString;
//        temp.m_line = simplified_line;
//        res_collections.push_back(temp);

//        studio_line_c_destroy(&gauss_line_c);
//        studio_line_c_destroy(&vac_gauss_line_c);

//        break;
//    }
//    std::string output_path;
// #ifdef IS_WINDOWS
//    output_path = "D:/5_file/2_readfile/geojson/multi_point/fitting_examples_res_1.geojson";
// #else
//    output_path = "/home/wyj/myself/2_data/2_geojson/multi_point/fitting_examples_res_1_c.geojson";
// #endif
//    studio_geo_utils::write_geo_coll(output_path, res_collections);

//    studio_geo_utils::destroy_gdal_env();

//    return 0;
//}